//
//  NickNameViewController.m
//  hello mui
//
//  Created by zhangtao on 15/9/14.
//  Copyright (c) 2015年 zt.td. All rights reserved.
//

#import "NickNameViewController.h"

@interface NickNameViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property(nonatomic,strong)UITextField *nickText;
@end

@implementation NickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.nickText.delegate = self;
    
    [self initStyle];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [_nickText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(void)initStyle{
    self.view.backgroundColor = [UIColor colorWithRed:240/255.f green:240/255.f blue:240/255.f alpha:1];
    
    UIView *textView = [[UIView alloc]initWithFrame:CGRectMake(20, 90,[[UIScreen mainScreen]bounds].size.width - 40 , 40)];
    textView.backgroundColor = [UIColor whiteColor];
    textView.layer.cornerRadius = 5;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor =rgb_box;
    
    _nickText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0,textView.frame.size.width-15, 40)];
    _nickText.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nickText.backgroundColor = [UIColor whiteColor];
   _nickText.placeholder = @"请输入昵称";
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(MAIN_WIDTH-10, 30, 30, 30)];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];

    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.layer.cornerRadius = 5;
    
    [rightButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
    rightButton.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.title = @"昵称";
    
  
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 150, [[UIScreen mainScreen]bounds].size.width - 40, 30)];
    label.text = @"";  //@"一天之内只能修改一次哦~";
    [label setTextColor:[UIColor grayColor]];
    
    
    [self.view addSubview:label];
    [self.view addSubview:textView];
    [self.view addSubview:rightButton];
    [textView addSubview:_nickText];
}

-(void)submit{
    NSLog(@"~~~~~~");
    
    
    //    NSMutableDictionary *dics = [NSMutableDictionary dictionaryWithObjectsAndKeys:userId,@"userId",self.nickText.text,@"nickName", nil];
    //    NSLog(@"dic==%@",dics);
    //    NSLog(@"md5%@",MD5);
    NSString *path = @"login/updateUser";
    
        NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
    
    NSDictionary *dic = @{@"userId":userId,@"nickName":_nickText.text};
    NSDictionary *dics = @{@"userId":NSUSER_DEF(@"USERID"),@"nickName":_nickText.text,@"bd_sig":[ToolObjc md5Str:dic]};
    NSUSER_DEF_NORSET(_nickText.text, @"NICKNAME");
    [[HttpConnctionManager sharedInstance]requestParam:dics andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        NSLog(@"~~~");
//        NSLog(@"~~~~~~%@,%@",[NSString stringWithFormat:@"%@",[succeedResult stringWithFilted:@"resCode"]],[succeedResult stringWithFilted:@"resMessage"]);
//        NSLog(@"%@",succeedResult);
        _infoVC.nickName.text = _nickText.text;
        [self.navigationController popViewControllerAnimated:YES];
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        NSLog(@"error::%@",error);
    }];
    
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"~~~");
    if (range.length>=15) {
        return NO;
//        [ToolObjc showMessage:@"您的昵称太长啦"];
    }else{
        return YES;
    }
}

-(void)textViewDidChange:(UITextView *)textView{
    NSLog(@"view");
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.nickText) {
        if (textField.text.length > 15) {
            [ToolObjc showMessage:@"最多只能输入15个汉字哦"];
            textField.text = [textField.text substringToIndex:15];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
